package com.sais.manage.boot.building.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sais.manage.boot.building.dto.BuildingDTO;
import com.sais.manage.boot.building.dto.BuildingSaveDTO;
import com.sais.manage.boot.building.dto.BuildingSearchDTO;
import com.sais.manage.boot.building.entity.Building;
import com.sais.manage.boot.building.mapper.BuildingMapper;
import com.sais.manage.boot.building.service.IBuildingService;
import com.sais.common.utils.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

;

/**
 * 宿舍管理系统-宿舍楼service 实现类
 *
 * @author zhang
 * @date 2019/11/30
 **/
@Service
@Slf4j
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> implements IBuildingService {

    @Resource
    private BuildingMapper buildingMapper;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean saveBuilding(BuildingSaveDTO buildingSaveDTO) {
        String ukId = buildingSaveDTO.getUkId();
        if (StringUtils.isEmpty(ukId)) {
            createBuilding(buildingSaveDTO);
        } else {
            updateBuilding(buildingSaveDTO);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean createBuilding(BuildingSaveDTO buildingSaveDTO) {
        if (StringUtils.isEmpty(buildingSaveDTO.getSort())) {
            buildingSaveDTO.setSort(10000);
        }
        Building building = buildingSaveDTO.convert();
        building.setUkId(UUIDUtils.getLower());
        // 添加宿舍楼信息
        buildingMapper.createBuilding(building);
        return true;
    }


    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateBuilding(BuildingSaveDTO buildingSaveDTO) {
        Building building = buildingSaveDTO.convert();
        building.setGmtModified(LocalDateTime.now());
        buildingMapper.updateById(buildingSaveDTO.convert());
        return true;

    }


    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteBuilding(String buildingId) {
        buildingMapper.deleteBuilding(buildingId);
        return true;
    }


    @Override
    public BuildingDTO selectBuilding(String buildingId) {
        BuildingDTO buildingDTO = buildingMapper.selectBuilding(buildingId);
        return buildingDTO;
    }


    @Override
    public Page<BuildingDTO> listBuilding(Integer current, Integer size, BuildingSearchDTO buildingSearchDTO) {
        Page<BuildingDTO> page = new Page<>(current, size);
        return buildingMapper.listBuilding(page, buildingSearchDTO);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean bindBuildingUser(String buildingId, List<String> userIds) {
        buildingMapper.bindBuildingUser(buildingId, userIds);
        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean unbindBuildingUser(String buildingId, List<String> userIds) {
        buildingMapper.unbindBuildingUser(buildingId, userIds);
        return true;
    }

    @Override
    public List<String> listBuildingType(Integer gender) {
        List<String> types = buildingMapper.listBuildingType(gender);
        return types;
    }

    @Override
    public BuildingDTO selectBuildingByCurrentUserId(String userId) {
        BuildingDTO building = buildingMapper.selectBuildingByCurrentUserId(userId);
        return building;
    }
}
